package com.dci.config.db;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Package com.acp.dao
 * @Description: 数据源配置2
 * @author: gis-许高峰
 * @date: 2019/3/22 17:48
 * @Copyright: 姑苏区政府大数据中心
 */
@Configuration
@MapperScan(basePackages = {"com.dci.dao.ms"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class AcpMsMybatisConfig {
    @Autowired
    @Qualifier("acpMsDataSource")
    private DataSource acpMsDataSource;

    /**
     * 配置数据源
     *
     * @return factoryBean.getObject()
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(acpMsDataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/ms/*.xml"));
        return factoryBean.getObject();
    }

    /**
     * 配置数据源
     *
     * @return template 配置模板
     * @throws Exception
     */
    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
        return template;
    }

}
